package net.moao.netutils.utils;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class MapUtils {
    /**
     * MapStringString 获取 Key SetString
     * @param map MapStringString
     * @return Key SetString
     */
    public static Set<String> getKeysFromMap(Map<String, String> map) {
        if (map == null) {
            return new HashSet<>();
        }
        return new HashSet<>(map.keySet());
    }

    /**
     * MapStringString 获取 Value SetString
     * @param map MapStringString
     * @return Value SetString
     */
    public static Set<String> getValuesFromMap(Map<String, String> map) {
        if (map == null) {
            return new HashSet<>();
        }
        return new HashSet<>(map.values());
    }

    /**
     * 从两个集合（Set<String>）生成一个新的 Map<String, String>
     * @param keySet 键 SetString
     * @param valueSet 值 SetString
     * @return MapStringString
     */
    public static Map<String, String> createMapFromSets(Set<String> keySet, Set<String> valueSet) {
        if (keySet == null || valueSet == null || keySet.size() != valueSet.size()) {
            throw new IllegalArgumentException("Key set and value set must have the same size and cannot be null.");
        }

        Map<String, String> resultMap = new LinkedHashMap<>(); // 使用 LinkedHashMap 保持插入顺序
        Iterator<String> keyIterator = keySet.iterator();
        Iterator<String> valueIterator = valueSet.iterator();

        while (keyIterator.hasNext() && valueIterator.hasNext()) {
            String key = keyIterator.next();
            String value = valueIterator.next();
            resultMap.put(key, value);
        }

        return resultMap;
    }

}
